<?php


namespace app\common\model;


use think\Db;

class Questions extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_exam_ques';
    // 主键ID
    protected $pk = 'ques_id';

    /**
     * 返回列表
     * @param $cond 条件
     * @param $orderby 排序
     * @param $item 返回参数
     * @param $page 分页
     * @param $limit 页面条数
     *
     */
    function get_list($cond, $orderby="ques_id desc", $item="*", $limit)
    {
        $where = null;

        if(!empty($cond['ques_name'])){
            $where[] = ['question', 'like', '%'.$cond['ques_name'].'%'];
        }
        $is_check_added = false;
        $questions = [];
        if(!empty($cond['paper_id'])){
            $examModel = new Exam();
            $questions = $examModel
                ->alias('p')
                ->leftJoin('ddt_exam_paperques pq','p.paper_id = pq.paper_id')
                ->field('pq.ques_id')
                ->where('p.paper_id','=',$cond['paper_id'])
                ->column('pq.ques_id');
            $is_check_added = true;
        }
        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit)
            ->each(function($item) use($is_check_added,$questions){
//                去除反序列化后的空数组
                $item['option'] = array_filter(unserialize($item['option']));
                if($is_check_added){
                    $item['is_in_paper'] = in_array($item['ques_id'],$questions)?true:false;
                }
                return $item;
            });
        return $rs;
    }

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*',$orderby = "")
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->find();

        return $rs;
    }

    function get_list_by_paper($cond, $orderby="pq.sort desc", $item="*", $limit)
    {
        $where = null;

        if(!empty($cond['paper_id'])){
            $where[] = ['paper_id', '=', $cond['paper_id']];
        }
        if(!empty($cond['ques_name'])){
            $where[] = ['question', 'like', '%'.$cond['ques_name'].'%'];
        }

        $rs = $this
            ->alias('q')
            ->leftJoin('ddt_exam_paperques pq','pq.ques_id = q.ques_id')
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit)
            ->each(function($item){
//                去除反序列化后的空数组
                $item['option'] = array_filter(unserialize($item['option']));
                return $item;
            });
        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            if(!isset($params['add_time'])){
                $params['add_time'] = time();
            }
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->pk;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

}